package cn.com.csg.smartenergy.modules.biz.task.controller;

import cn.com.csg.smartenergy.modules.biz.task.entity.TaskEntity;
import cn.com.csg.smartenergy.modules.biz.task.service.TaskService;
import cn.com.csg.smartenergy.util.OperResult;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/task")
public class TaskController {

    @Autowired
    public RedisTemplate<String,String> redisTemplate;
    @Autowired
    public TaskService taskService;

    /**
     * 启动or移除定时任务（心跳定时任务）
     * @param taskEntity
     * @return
     */
    @SneakyThrows
    @ResponseBody
    @PostMapping("/taskStartOrRemove")
    public OperResult taskStartOrRemove(@RequestBody TaskEntity taskEntity){
        try {
            taskService.taskStartOrRemove(taskEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return OperResult.error(e.getMessage());
        }
        return OperResult.ok();
    }
    /**
     * 启动or移除定时任务（发送模拟数据）
     * @param taskEntity
     * @return
     */
    @SneakyThrows
    @ResponseBody
    @PostMapping("/sendMessages")
    public OperResult sendMessages(@RequestBody TaskEntity taskEntity){
        try {
            taskService.sendMessages(taskEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return OperResult.error(e.getMessage());
        }
        return OperResult.ok();
    }

}
